/**
 * Created by yy on 16-8-6.
 */
angular.module('auth', ['angular-md5', 'ngCookies'])
    .factory('hash', function (md5) {
        return function (v1, v2) {
            return md5.createHash('hash-it-happily-' + v1 + '$' + v2);
        }
    })
    .factory('login', function ($http, hash) {
        var postUrl = 'http://115.159.71.32:20161/ecust-apis/account-secure/account-login';
        return function (username, password, callback) {
            if (username == '' || password == '') {
                return callback(2);
            }
            password = hash(username, password);
            var postData = {uid: username, pwd: password};
            var promise = $http.post(postUrl, postData);
            promise.then(function (res) {
                if (res.data.code == 0) {
                    callback(null, res.data);
                } else {
                    callback(1);
                }
            }, function (res) {
                callback(1);
            })
            }
        })
    .factory('setCredit', function ($cookies) {
        return function (data) {
            $cookies.put('token', data.token);
            $cookies.put('type', data.type);
        }
    })
    .factory('checkCredit', function ($cookies) {
        return function (type) {
            return $cookies.get('type') == type;x
        }
    })
    .factory('deleteCredit', function ($cookies) {
        return function () {
            $cookies.put('token', '');
            $cookies.put('type', '');
        }
    })
  .factory('getToken', function ($cookies) {
     return $cookies.get('token') ? $cookies.get('token') : '';
  });
